<?php
class Portal_LoginController extends Mediad2_Controller_Action
{	
	/**
	 * var
	 */
	private $autologin 		 = null;      //是否两周之内登陆的状态
	private $email     		 = null;
	private $pwd       		 = null;
	private $message;
	private $title;
	private $Reurl 		  	 = "login";
	private $Retitle 	  	 = "修改失败";
	private $Retime  	  	 = 50;
	
	private $serviceUser;
	
	public function init()
	{
		parent::init();
		$this->serviceUser = new Mediad2_Service_Admin_User();
		
		//email
		if (isset($this->httpdata['email'])) {
			$this->email = $this->httpdata['email'];
		}
		
		//pwd
		if (isset($this->httpdata['pwd'])) {
			$this->pwd = $this->httpdata['pwd'];
		}
	}
	
	
	//--------------------------------------------------
	// login 公开接口
	//--------------------------------------------------
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$this->_redirect(WEB_PATH."my/login");
		$cookie = $this->cookieMethod("get", "email_cookie");
		if (isset($cookie)) {
			$this->pageview->assign ("email_cookie",$cookie);
		}
		$this->helper->setPageView($this->pageview, WEB_TITLE, "login", null);
	}

	/**
	 * 忘记密码
	 */
	public function forgotpasswordAction()
	{
		//判断email是否为空
		if (empty($this->email)) {
			$this->message = "Missing parameter:email";
			$this->title   = "发送失败";
		}
		//得到用户信息
		$where = "email = '$this->email'";
		$result = $this->serviceUser->findBy($where);
		if ($result->getCode()==$result::CODE_FAILURE) {
			$this->message = "没有找到你的注册信息，请确认您填写的邮箱地址与注册时使用的一致";
			$this->title   = "发送失败";
		}
		//设置数据
		$user = $result->getData();
		$uid = $user->getUid();
		//判断用户是否存在
		if(!empty($uid)){
			$newUid = md5(md5($uid));
			$url = WEB_PATH."login/resetpassword?u=$newUid&email=$this->email";
			
			//发送邮件
			$emailService = new Mediad2_Service_Util_Email();
			if ($emailService->sendResetPasswordEmail($this->email,$url)) {
				//邮件发送成功
				$this->message = "密码重置邮件已发送至您的注册邮箱，请尽快去设置您的密码";
				$this->title   = "发送成功";
			} else {
				//邮件发送失败
				$this->message = "邮件发送失败，请联系客服...";
				$this->title   = "发送失败";
			}
		}else{
			$this->message = "没有找到你的注册信息，请确认您填写的邮箱地址与注册时使用的一致";
			$this->title   = "发送失败";
		}
		$Retime  	  = "5";//跳转时间
		$Reurl   	  = "login";
		$link_content = "点击这里直接返回首页";
		$this->pageview->assign("Retitle",$this->title);
		$this->pageview->assign("Retime",$Retime);
		$this->pageview->assign("Reurl",$Reurl);
		$this->pageview->assign("content",$this->message);
		$this->pageview->assign("link_content",$link_content);
		$this->helper->setPageView($this->pageview,WEB_TITLE,"success",null);
	}
	
	/**
	 * 重置密码页面
	 */
	public function resetpasswordAction()
	{
		if(isset($_GET['u']) && !empty($_GET['u'])){
		
			$uid = $_GET['u'];
			$email=$_GET['email'];
			$where = "email = '$email'";
			$result = $this->serviceUser->findBy($where);
			if($result->getCode() == $result::CODE_FAILURE){
				$message 	  = "网络繁忙,请稍后再试...";
				$link_content = "点击这里直接返回首页";
				$this->pageview->assign("Retitle",$this->Retitle);
				$this->pageview->assign("Retime",$this->Retime);
				$this->pageview->assign("content",$message);
				$this->pageview->assign("link_content",$link_content);
				$this->pageview->assign("Reurl",$this->Reurl);
				$this->helper->setPageView($this->pageview,WEB_TITLE,"success",null);
			}else{
		
				$user = $result->getData();
				$getUid = $user->getUid();
		
				if($uid !=md5(md5($getUid))){
					$message = "地址不正确,请重新获取链接地址...";
					$link_content = "点击这里直接返回首页";
					$this->pageview->assign("Retitle",$this->Retitle);
					$this->pageview->assign("Retime",$this->Retime);
					$this->pageview->assign("content",$message);
					$this->pageview->assign("link_content",$link_content);
					$this->pageview->assign("Reurl",$this->Reurl);
					$this->helper->setPageView($this->pageview,WEB_TITLE,"success",null);
				}else{
					$this->pageview->assign("email",$email); 
					$this->helper->setPageView( $this->pageview, WEB_TITLE, "updatepassword", null);
				}
			}
		}
	}
	/**
	 * 新密码验证
	 */
	public function verifynewpasswordAction()
	{
			$password = md5($this->pwd);
			//验证数据的真实性
			$service = new Mediad2_Service_Admin_User();
			$where = "email = '$this->email'";
			$result = $service->findBy($where);
			if($result->getCode() == $result::CODE_FAILURE){
				//连接失败
				$this->message = "系统繁忙,请稍后再试...";
				$this->title   = "修改失败";
			}
			//连接成功则执行
			$user = $result->getData();
			$getUid = $user->getUid();
			if(!isset($getUid)){
				//若用户不存在，则不能修改
				$this->message = "用户不存在,请重新获取链接...";
				$this->title   = "修改失败";
			}else{
				//若用户存在修改密码
				$user->setPwd($password);
				$re = $service->update($user);
	
				if($re->getCode() == $re::CODE_FAILURE){
					//链接失败
					$this->message = "系统繁忙,请稍后再试...";
					$this->title   = "修改失败";
				}else{
					//修改成功
					$this->message = "恭喜您密码修改成功";
					$this->title   = "修改成功";
				}
			}
			$Retime = "5";
			$link_content = "点击这里直接返回首页";
			$this->pageview->assign("Retitle",$this->title);
			$this->pageview->assign("Retime",$Retime);
			$this->pageview->assign("content",$this->message);
			$this->pageview->assign("link_content",$link_content);
			$this->pageview->assign("Reurl",$this->Reurl);
			$this->helper->setPageView($this->pageview,WEB_TITLE,"success",null);
	}
		
	//--------------------------------------------------
	// 私有方法
	//--------------------------------------------------
	
	public function loginAction(){
		$this->loginSystem();
	}
	
	/**
	 * login system
	 */
	private function loginSystem()
	{
		//var
		if ((isset($this->httpdata['autologin'])) && (!empty($this->httpdata['autologin'])) ) {
			$this->autologin = $this->httpdata['autologin'];
		}
		$issuccess = null;	//默认为成功
		//登录
		$result = $this->serviceUser->loginByEmail($this->email, $this->pwd);
		if ($result->getCode() == $result::CODE_SUCCESS) {
			$registry = Zend_Registry::getInstance();
			$haveUser = $registry->auth->hasIdentity();
			$identity =  $registry->auth->getIdentity();
			//判断是否记住我，并保存email
			if(!empty($this->httpdata['autologin'])){
				$this->loginCookieConfigure("save");
			}else{
				$this->loginCookieConfigure("del");
			}
			if ($haveUser) {
				if($identity->isadmin==1){
					$this->_redirect(WEB_PATH.'my/navigate');
				}else{
					$this->_redirect(WEB_PATH.'my/dashboard');
				}
			}else{
				$issuccess = "2";
			}
			//成功登陆
		}else{
			//用户不存在
			$issuccess = "2";
		}
		$this->pageview->assign ("issuccess",$issuccess);
		$this->helper->setPageView($this->pageview, WEB_TITLE, "login", null);
	}
	
	/*
	 * 保存cookie和删除cookie的私有方法
	 */
	private function loginCookieConfigure($act,$pwd = '')
	{
		switch ($act) {
			case 'save':
				$this->cookieMethod($act, 'email_cookie', $this->email);
				break;
			case 'del':
				$this->cookieMethod($act, "email_cookie");
				break;
		}
	}
}